package com.tecnoagrosoft.zafra.control.activity;



import com.tecnoagrosoft.zafra.control.activity.R;
import com.google.android.gms.gcm.GoogleCloudMessaging;

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;

public class GCMIntentService extends IntentService 
{
	private static final int NOTIF_ALERTA_ID = 1;

	public GCMIntentService() {
        super("GCMIntentService");
    }

	@Override
    protected void onHandleIntent(Intent intent) 
	{
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
        
        String messageType = gcm.getMessageType(intent);
        Bundle extras = intent.getExtras();

        if (!extras.isEmpty()) 
        {  
            if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) 
            {
            	int code_mesaje= Integer.parseInt(intent.getExtras().getString("code_mesaje"));
        		String tableid = intent.getExtras().getString("table_id");
        		String tablaName = intent.getExtras().getString("table_name");
        		String title = intent.getExtras().getString("title");
        		String mensaje = intent.getExtras().getString("mess");
        		String extra = intent.getExtras().getString("extra");
            	
            	mostrarNotification(code_mesaje,tablaName,tableid, title,mensaje,extra);
            }
        }
        
        GCMBroadcastReceiver.completeWakefulIntent(intent);
    }

	private void mostrarNotification(int codeMesaje,String tableName,String tablaid,String title, String message ,String mContentInfo) 
	{
		NotificationManager mNotificationManager =    
				(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

		
		Intent notIntent =  new Intent(getApplicationContext(), HomeActivity.class);
		int logo =R.drawable.logo_sin;

		String mTicker=getString(R.string.men_new_notificacion) ;
		switch (codeMesaje) {
		case 0:
			//por cuando el mensaje es cero signfica que no existe un tipo de mensaje definido
			notIntent= new Intent(getApplicationContext(), HomeActivity.class);
			break;
			
		case 1000:
			//definido 
			notIntent= new Intent(getApplicationContext(), HomeActivity.class);
			notIntent.putExtra("table_name", tableName );
			notIntent.putExtra("table_id", tablaid );
			logo =R.drawable.logo_sin;
			break;
		case 1001:  //danger
			//definido 
			notIntent= new Intent(getApplicationContext(), HomeActivity.class);
			logo =android.R.drawable.ic_delete;
			break;	
			
		case 1002: //waring
			//sugerencias
//			notIntent= new Intent(getApplicationContext(), SugerenciaActivity.class);
			notIntent.putExtra("idSugerenciaNO", tablaid );
			logo =android.R.drawable.ic_dialog_alert;
			break;	
		case 1003: //success
			//evento 
//			notIntent= new Intent(getApplicationContext(), EventoActivity.class);
			logo =R.drawable.ic_action_accept;
			notIntent.putExtra("idEventoNO", tablaid );
			break;
		case 1004: //info
			//evento 
//			notIntent= new Intent(getApplicationContext(), EventoActivity.class);
			logo =R.drawable.ic_action_about;
			notIntent.putExtra("idEventoNO", tablaid );
			break;
			
		case 1005: //SINCRONIZAR
			notIntent= new Intent(getApplicationContext(), HomeActivity.class);
			notIntent.setAction("SINCRONIZAR");
			logo =R.drawable.logo_sin;
			break;
			
		case 1006: //NOTIFICACCION
			notIntent= new Intent(getApplicationContext(), HomeActivity.class);
			notIntent.setAction("NOTIFICACION");
			logo =R.drawable.logo_sin;
			break;
			
		case 1007: //ACTIVIDADES
			notIntent= new Intent(getApplicationContext(), HomeActivity.class);
			notIntent.setAction("ACTIVIDADES");
			logo =R.drawable.logo_sin;
			break;
			
		case 1008: //CLIMA
			notIntent= new Intent(getApplicationContext(), HomeActivity.class);
			notIntent.setAction("CLIMA");
			logo =R.drawable.logo_sin;
			break;
			
		case 1009: //PRECIO_CHICAGO
			notIntent= new Intent(getApplicationContext(), HomeActivity.class);
			notIntent.setAction("PRECIO_CHICAGO");
			logo =R.drawable.logo_sin;
			break;
			
		case 1010: //RESUMEN_SEGUIMIENTO
			notIntent= new Intent(getApplicationContext(), HomeActivity.class);
			notIntent.setAction("RESUMEN_SEGUIMIENTO");
			logo =R.drawable.logo_sin;
			break;
			
			
		default:
			notIntent= new Intent(getApplicationContext(), HomeActivity.class);
			break;
		}

		
		NotificationCompat.Builder mBuilder = 
			new NotificationCompat.Builder(this)  
				.setSmallIcon(logo)  
				.setContentTitle(title)
				.setContentText(message)
				.setDefaults(Notification.DEFAULT_ALL)
				.setContentInfo(mContentInfo)
				.setTicker(mTicker);

		
		
		
		//se setera es flap al intent para no iniciar un nueva instancia
		notIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
		PendingIntent contIntent = PendingIntent.getActivity(     
				this, 0, notIntent, 0);   

		mBuilder.setContentIntent(contIntent);

		mNotificationManager.notify(NOTIF_ALERTA_ID, mBuilder.build());
    }
}